package com.jzr.task.job.kangying;

import com.jzr.task.db1.service.KangYingService;
import com.jzr.task.db1.vo.KyAreaOfficeVo;
import com.jzr.task.db1.vo.KyAreaVo;
import com.jzr.task.db1.vo.KyOfficeVo;
import com.jzr.task.job.BaseJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component("KangYing.SyncAreaOffice")
public class KySyncAreaOffice extends BaseJob {
    @Value("${kangying.apiUrl}")
    private String apiUrl;

    @Autowired
    private KangYingService kangYingService;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void process() throws Exception {
        //同步Area
        List<KyAreaVo> list = kangYingService.getAreaList(null);
        if(list != null && list.size()>0) {
            KyResult result = KyRestTemplateUtil.postJson(restTemplate, apiUrl + "Area/AreaListSync", list, KyResult.class);
            if (result == null) {
                throw new Exception("同步康英Area时无法获取返回结果");
            }
            if (result.getStatus() == -1) {
                throw new Exception("同步康英Area失败," + result.getMessage() + "," + result.getContent());
            }
        }

        //同步Office
        List<KyOfficeVo> list2 = kangYingService.getOfficeList(null);
        if(list2 != null && list2.size()>0) {
            KyResult result = KyRestTemplateUtil.postJson(restTemplate, apiUrl + "Office/OfficeSync", list2, KyResult.class);
            if (result == null) {
                throw new Exception("同步康英Office时无法获取返回结果");
            }
            if (result.getStatus() == -1) {
                throw new Exception("同步康英Office失败," + result.getMessage() + "," + result.getContent());
            }
        }

        List<KyAreaOfficeVo> list3 = kangYingService.getAreaOfficeList(null);
        if(list2 != null && list3.size()>0) {
            KyResult result = KyRestTemplateUtil.postJson(restTemplate, apiUrl + "Area/AreaListLinkOfficeSync", list3, KyResult.class);
            if (result == null) {
                throw new Exception("同步康英AreaOffice时无法获取返回结果");
            }
            if (result.getStatus() == -1) {
                throw new Exception("同步康英AreaOffice失败," + result.getMessage() + "," + result.getContent());
            }
            if(result.getContent() != null && result.getContent().indexOf("科室或区域错误") != -1){
                throw new Exception("同步康英AreaOffice失败,科室或区域错误");
            }
        }

    }
}
